<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Cookie;

/**
 * By：wl
 * Date: 2018.11
 * Function: 公共继承
 */

class Base extends Controller
{
    
    public function _initialize()
    {
       
        //登录判断  BaseindexController
        $uid = Cookie::get('admin_userid_tp5');

        if($uid){
            $admin_user_info = Session::get('admin_userinfo_tp5'.$uid);

            if (!$admin_user_info) {
                Cookie::delete('admin_userid_tp5');
                $this->redirect("admin/Admin/login");
            }else{
                $this->assign("admin_user_info",$admin_user_info);  
            }
        }else{
            $this->redirect("admin/Admin/login");
        }



        //获取面包屑导航
        $controller = request()->controller();
        if($controller != 'Index'){
            $this->assign("controller",$controller);
            $bread_nav = '';

            $res_1 = $this->get_pid_name($controller);
            $bread_nav .= "<a>".$res_1['name']."</a>";

            if($res_1['pid']){
                $res = $this->pid_name($res_1['pid']);
                $bread_nav = "<a href=''>".$res['name']."</a>".$bread_nav;
            }
            $bread_nav = '<a href="">首页</a>'.$bread_nav.'<a><cite>'.$res_1['now_name'].'</cite></a>';
            $this->assign("bread_nav",$bread_nav);
        }
        


    }
    /* 退出登录*/
    public function outlogin()
    {
        $uid = Cookie::get('admin_userid_tp5');
        //删Cookie、Session
        Cookie::delete('admin_userid_tp5');
        Session::set('admin_userinfo_tp5'.$uid,'');

        $this->redirect("admin/Admin/login");
    }

    //控制器名查父级name
    function get_pid_name($controller)
    {
        $info = Db::query("select * from admin_menu where controller='$controller'")[0];
        if($info['pid']){
            $pid = $info['pid'];
            $pid_info =  Db::query("select pid,name from admin_menu where id = $pid");
            $pid_info[0]['now_name'] = $info['name'];
            return $pid_info[0];
        }
        return true;
    }

    //pid查父级name
    function pid_name($pid)
    {
        $pid_info =  Db::query("select pid,name from admin_menu where id = $pid");
        return $pid_info[0];
    }


}